/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "widgets_init.h"
#include "nrf_process.h"
#include "custom.h"



void msg_pos_sw(lv_ui *ui)
{
    if (!guider_ui.msg_intensity_pos.foot) {
        guider_ui.msg_intensity_pos.foot = true;
        lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
        __disable_irq();
        bool foot_pos_setup = guider_ui.msg_intensity_pos.foot;
        uint8_t bits_foot_ins0 = backlight.bits.msg_foot_ins0;
        uint8_t bits_foot_ins1 = backlight.bits.msg_foot_ins1;
        uint8_t bits_foot_ins2 = backlight.bits.msg_foot_ins2;
        __enable_irq();

        if (ui && ui->massage_intensity_roller_1) {
            if (bits_foot_ins0 && foot_pos_setup) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 0, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            } else if (bits_foot_ins1 && foot_pos_setup) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 1, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            } else if (bits_foot_ins2 && foot_pos_setup) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 2, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            }
        }

    } else {
        guider_ui.msg_intensity_pos.foot = false;
        lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
        __disable_irq();
        bool foot_pos_foot = guider_ui.msg_intensity_pos.foot;
        uint8_t bits_head_ins0 = backlight.bits.msg_head_ins0;
        uint8_t bits_head_ins1 = backlight.bits.msg_head_ins1;
        uint8_t bits_head_ins2 = backlight.bits.msg_head_ins2;
        __enable_irq();

        if (ui && ui->massage_intensity_roller_1) {
            if (bits_head_ins0 && !foot_pos_foot) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 0, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            } else if (bits_head_ins1 && !foot_pos_foot) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 1, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            } else if (bits_head_ins2 && !foot_pos_foot) {
                lv_roller_set_selected(ui->massage_intensity_roller_1, 2, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            }
        }
    }
}


static void mask_event_cb(lv_event_t *e)
{
    #if 0
    lv_obj_t *roller = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    // 获取当前被选中的索引（第几项，从 0 开始）
    uint16_t sel_id = lv_roller_get_selected(roller);
    switch (code) {
        case LV_EVENT_PRESSED:
        {
            nrf_para.nrf.start_send_flag = true;
            // 设置按下时的样式

            lv_obj_set_style_bg_color(roller, lv_color_hex(guider_ui.color.backlight), LV_PART_SELECTED);
            lv_obj_set_style_text_color(roller, lv_color_hex(0x2195f6), LV_PART_SELECTED);
            if (!guider_ui.msg_intensity_pos.foot) {
                if(sel_id == 0) {
                    nrf_para.nrf.send_cmd = MASG_HEAD_INS1_CMD;
                } else if (sel_id == 1) {
                    nrf_para.nrf.send_cmd = MASG_HEAD_INS2_CMD;
                } else if (sel_id == 2) {
                    nrf_para.nrf.send_cmd = MASG_HEAD_INS3_CMD;
                }
            } else {
                if(sel_id == 0) {
                    nrf_para.nrf.send_cmd = MASG_FOOT_INS1_CMD;
                } else if (sel_id == 1) {
                    nrf_para.nrf.send_cmd = MASG_FOOT_INS2_CMD;
                } else if (sel_id == 2) {
                    nrf_para.nrf.send_cmd = MASG_FOOT_INS3_CMD;
                }
            }
            break;
        }
        case LV_EVENT_RELEASED:
        {
            lv_obj_set_style_bg_color(roller, lv_color_hex(guider_ui.color.backlight), LV_PART_SELECTED);
            lv_obj_set_style_text_color(roller, lv_color_hex(0xffffff), LV_PART_SELECTED);
            break;
        }
        default:
            break;
    }
    #endif
}

void setup_scr_massage_intensity(lv_ui *ui)
{
    //Write codes massage_intensity
    ui->massage_intensity = lv_obj_create(NULL);
    lv_obj_set_size(ui->massage_intensity, 240, 320);
    lv_obj_set_scrollbar_mode(ui->massage_intensity, LV_SCROLLBAR_MODE_OFF);

    //Write style for massage_intensity, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_bg_opa(ui->massage_intensity, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->massage_intensity, lv_color_hex(guider_ui.color.backlight), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->massage_intensity, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);


   //Write codes motion_1_label_1
    ui->massage_intensity_label_1 = lv_label_create(ui->massage_intensity);
    lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
    lv_label_set_long_mode(ui->massage_intensity_label_1, LV_LABEL_LONG_WRAP);
    lv_obj_set_pos(ui->massage_intensity_label_1, 0, 15);
    lv_obj_set_size(ui->massage_intensity_label_1, 240, 31);

    //Write style for motion_1_label_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_border_width(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->massage_intensity_label_1, lv_color_hex(0xfefefe), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->massage_intensity_label_1, &lv_font_montserrat_18, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->massage_intensity_label_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_letter_space(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_line_space(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->massage_intensity_label_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui->massage_intensity_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);

    //Write codes massage_intensity_roller_1
    ui->massage_intensity_roller_1 = lv_roller_create(ui->massage_intensity);
    lv_roller_set_options(ui->massage_intensity_roller_1, "1\n2\n3", LV_ROLLER_MODE_INFINITE);
    lv_obj_set_pos(ui->massage_intensity_roller_1, 0, 119);
    lv_obj_set_width(ui->massage_intensity_roller_1, 240);

    //Write style for massage_mode_roller_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_radius(ui->massage_intensity_roller_1, 5, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui->massage_intensity_roller_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->massage_intensity_roller_1, lv_color_hex(guider_ui.color.backlight), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->massage_intensity_roller_1, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->massage_intensity_roller_1, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->massage_intensity_roller_1, &lv_font_montserrat_14, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->massage_intensity_roller_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->massage_intensity_roller_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui->massage_intensity_roller_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui->massage_intensity_roller_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);

    //Write style for massage_mode_roller_1, Part: LV_PART_SELECTED, State: LV_STATE_DEFAULT.
    lv_obj_set_style_bg_opa(ui->massage_intensity_roller_1, 255, LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->massage_intensity_roller_1, lv_color_hex(guider_ui.color.backlight), LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->massage_intensity_roller_1, LV_GRAD_DIR_NONE, LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->massage_intensity_roller_1, lv_color_hex(0x2195f6), LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->massage_intensity_roller_1, &lv_font_montserrat_18, LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->massage_intensity_roller_1, 255, LV_PART_SELECTED|LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->massage_intensity_roller_1, LV_TEXT_ALIGN_CENTER, LV_PART_SELECTED|LV_STATE_DEFAULT);

    lv_roller_set_visible_row_count(ui->massage_intensity_roller_1, 3);

    __disable_irq();
    bool foot_pos_foot = guider_ui.msg_intensity_pos.foot;
    uint8_t bits_head_ins0 = backlight.bits.msg_head_ins0;
    uint8_t bits_head_ins1 = backlight.bits.msg_head_ins1;
    uint8_t bits_head_ins2 = backlight.bits.msg_head_ins2;
    __enable_irq();

    if (ui && ui->massage_intensity_roller_1) {
        if (bits_head_ins0 && !foot_pos_foot) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 0, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
        } else if (bits_head_ins1 && !foot_pos_foot) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 1, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
        } else if (bits_head_ins2 && !foot_pos_foot) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 2, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
        }
    }

    __disable_irq();
    bool foot_pos_setup = guider_ui.msg_intensity_pos.foot;
    uint8_t bits_foot_ins0 = backlight.bits.msg_foot_ins0;
    uint8_t bits_foot_ins1 = backlight.bits.msg_foot_ins1;
    uint8_t bits_foot_ins2 = backlight.bits.msg_foot_ins2;
    __enable_irq();

    if (ui && ui->massage_intensity_roller_1) {
        if (bits_foot_ins0 && foot_pos_setup) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 0, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
        } else if (bits_foot_ins1 && foot_pos_setup) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 1, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
        } else if (bits_foot_ins2 && foot_pos_setup) {
            lv_roller_set_selected(ui->massage_intensity_roller_1, 2, LV_ANIM_OFF);
            lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
        }
    }

    //The custom code of massage_intensity.
    // 创建并设置 group
    lv_group_t *main_group = lv_group_create();
    lv_group_add_obj(main_group, ui->massage_intensity_roller_1);
    lv_indev_set_group(indev_keypad, main_group);

    // lv_obj_add_event_cb(ui->massage_intensity_roller_1, mask_event_cb, LV_EVENT_ALL, NULL);


    //Update current screen layout.
    lv_obj_update_layout(ui->massage_intensity);

}

